'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskreportdetailCtrl
 * @description
 * # TaskreportdetailCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('TaskreportdetailCtrl', function(DataService, localStorageService, openApi, $routeParams, Util, $location) {
        var vm = this;
        // 根据类型判断是否显示评论，草稿则不显示
        var reportpicker = localStorageService.get("reportpicker");
        var type = vm.type = $routeParams.type;
        var pkId = $routeParams.pkId
        var initData = function() {
            setReadReoprt(pkId);
            getDetailData();
        };
        this.editReport = function() {
            Util.setformKey("Personnel_WorkReport")
            $location.path("task/newreport/" + pkId).replace();
        };
        this.makeRead = function() {
            DataService.getData(openApi.setTaskWork,{contentId:pkId}).then(function(){
                vm.data.confirmRead=true
            })
        };
        // 删除评论
        this.delComment = function(data, index) {
                var param = {};
                param.pkId = data.pkId
                DataService.getData(openApi.delReplyReport, param).then(function(data) {
                    vm.data.actions.splice(index, 1);
                })
            }
            // 上一条/下一条数据
        this.getPrevOrNextData = function(action) {
                var param = reportpicker || {};
                param.action = action;
                param.flag = type;
                pkId = param.pkId = this.data.pkId;
                DataService.getData(openApi.getNextOrPrev, param).then(function(data) {
                    if (data) {
                        vm.data = data;
                        setReadReoprt(data.pkId);
                    } else {
                        var tip = action == "prev" ? "第一条报告" : "最后一条报告"
                        Util.Toast("当前报告为" + tip)
                    }
                    console.log(data)
                })
            }
            // 针对草稿，可以直接提交
        this.submit = function() {
            this.data.follows = this.data.followList.map(function(item, index) {
                return item.userId;
            })
            var param = angular.copy(this.data);
            delete param.followList;
            // 提交是1，草稿是0
            param.status = 1;
            DataService.getData(openApi.updateReport, param).then(function(data) {
                Util.Toast(data, { callback: Util.goBack });
            })
        };
        // 确认删除草稿
        this.confirmdel = function() {
                navigator.notification.confirm(
                    '是否删除',
                    vm.delReport,
                    '提示', ['删除', '取消']
                )

        };
        this.delReport = function() {
            var param = {};
            param.pkId = pkId;
            DataService.getData(openApi.delReport, param).then(function(data) {
                Util.Toast(data, { callback: Util.goBack });
            })
        }
        var getDetailData = function() {
                var param = {};
                param.pkId = pkId;
                DataService.getData(openApi.getworklogDetail, param).then(function(data) {
                    if (data) {
                        vm.data = data;
                    } else {
                        Util.Toast("您无权限查看这条记录", { callback: Util.goBack })
                    }
                    console.log(data)
                })
            }
            // 设置已阅
        var setReadReoprt = function(pkId) {
                // 说明是未阅要设置已阅
                if (type == "3") {
                    DataService.getData(openApi.ReadworkReoprt, { contentId: pkId, noloader: true })
                }
            }
            // 初始化数据
        initData();
    });
